/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jobportal.student;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import java.util.Random;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import jobportal.Dbconn;
import jobportal.EmailSender;

public class StudentRegister extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        PrintWriter out = response.getWriter();
        try {
            HttpSession session = request.getSession();
            Connection con = Dbconn.getConn();
            Statement st = con.createStatement();
            ResultSet rs;
            String sql;
            if (request.getParameter("captcha") == null || !session.getAttribute("captcha").equals(request.getParameter("captcha"))) {
                response.sendRedirect("student_registration.jsp?Error=Invalid captcha value");
            }else{
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            String email = request.getParameter("jsemail");
            String name = request.getParameter("name");
            String gender = request.getParameter("gender");
            String dob = request.getParameter("dob");
            String nationality = request.getParameter("nationality");
            String mobile = request.getParameter("mobile");
            String cllgname = request.getParameter("cllgname");
            String address=request.getParameter("address");
            String state=request.getParameter("state");
            String city=request.getParameter("city");
            out.println(cllgname);
            Random rd = new Random();
            long l = rd.nextLong();
            if (l < 0) {
                l *= -1;
            }
            sql = "insert into student(username,password,email,name,gender,dob,contact,nationality,clgid,emailstatus,address,state,city) "
                    + "values('" + username + "','" + password + "','" + email + "','" + name + "','" + gender + "','" + dob + "','" + mobile + "','" + nationality + "','" + cllgname + "',"+l+ ",'"+address+ "','" +state+ "','" +city+"')";
            int i = st.executeUpdate(sql);
            if (i == 0) {
                out.println("Registration Failed");
            } else {
                out.println("Registration Successful");
                rs = st.executeQuery("select * from emailmessage where id=1");
                if (rs.next()) {
                    String subject = rs.getString("subject");
                    String content = rs.getString("content");
                    content = content.replace("%uname%", name);
                    content = content.replace("%username%", username);
                    content = content.replace("%password%", password);


                    content = content.replace("%vercode%", l + "");
                    EmailSender.sendMail(email, subject, content);
                }
                response.sendRedirect("StudentLogin?username=" + username + "&password=" + password);
            }
            }
        } catch (Exception e) {
            out.println("Exception::" + e);
            if(e.toString().contains("Duplicate entry")){
                response.sendRedirect("student_registration.jsp?user=User Already Exist");
            }else{
                response.sendRedirect("student_registration.jsp?ex="+e);
            }
        }

    }
}
